package com.czz.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.portal.entity.Content;
import com.czz.portal.entity.Question;
import com.czz.portal.mapper.ContentMapper;
import com.czz.portal.mapper.QuestionMapper;
import com.czz.portal.query.QuestionQuery;
import com.czz.portal.service.QuestionService;
import com.czz.portal.vo.QuestionDetailVo;
import com.czz.portal.vo.RecommendQuestionVo;
import com.czz.portal.vo.UserQuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: QuestionServiceImpl
 * @Author Czz
 * @Date 2020/2/13
 * @Time 13:28
 **/
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    ContentMapper contentMapper;

    @Override
    public List<Question> getQuestionPageList(Page<Question> pageParam, QuestionQuery questionQuery) {
        return questionMapper.getQuestionPageList(pageParam,questionQuery);
    }

    @Override
    public QuestionDetailVo getQuestionDetailById(String id) {
        return questionMapper.getQuestionDetailById(id);
    }

    @Override
    public List<RecommendQuestionVo> getRecommendQuestion(String id) {
        List<RecommendQuestionVo> recommendList = new ArrayList<>();
        Question question = questionMapper.selectById(id);

        String[] tagList = question.getTag().split(",");
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();

        for (int i = 0; i < tagList.length; i++){
            queryWrapper.or().eq("question_tag",tagList[i]);
        }
        List<Question> questionList = questionMapper.selectList(queryWrapper);

        for (int j = 0; j < questionList.size(); j++){
            Question temp = questionList.get(j);
            RecommendQuestionVo vo = new RecommendQuestionVo();
            vo.setId(temp.getQuestionId());
            vo.setTitle(temp.getTitle());
            recommendList.add(vo);
        }
        queryWrapper.last("order by RAND() limit 0,5");
        return recommendList;
    }

    @Override
    public void updateQuestionClickById(String id) {
        questionMapper.updateQuestionClickById(id);
    }


    @Override
    @Transactional
    public void saveQuestion(Question question, Content content) {
        //插入question表
        questionMapper.insert(question);

        //插入content表
        content.setQuestionId(question.getQuestionId());
        contentMapper.insert(content);

        //更新question表中content
        question.setContentId(content.getContentId());
        questionMapper.updateById(question);
    }

    @Override
    public List<UserQuestionVo> getUserQuestionPageList(Page<UserQuestionVo> pageParam, String userId) {
        return  questionMapper.getUserQuestionPageList(pageParam,userId);
    }
}
